package org.example.service;

import org.example.exception.BusinessException;
import org.example.mapper.SettingMapper;
import org.example.model.Award;
import org.example.model.Member;
import org.example.model.Setting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SettingService {

    @Autowired
    private SettingMapper settingMapper;

    @Autowired
    private AwardService awardService;
    @Autowired
    private MemberService memberService;

    public Setting queryByUserId(Integer id) {
        Setting setting = settingMapper.selectByUserId(id); // 把 setting 传进去, 有一个 用户一定有一个用户
        if(setting == null){
            throw new BusinessException("SET001","用户设置信息出错");
        }
        //查询奖品列表, 人员列表, 设置到属性中

//            // 设置查询奖品
//            Award award = new Award();
//            award.setSettingId(setting.getId());
//            List<Award> awards = awardService.queryBySettingId(id);
//            setting.setAwards(awards);
//
//            // 查询人员列表
//            Member member = new Member();
//            member.setSettingId(id);
//            List<Member> members = memberService.queryBySettingId(id);
//            setting.setMembers(member);
        return setting;
        // 再注册的时候 就把这个用户的设置传进来
        // 也就是 注册用户时 需要生成一个 setting 数据, 1对1. 如果没有生成, 业务有问题
    }


    @Transactional
    public int update(Integer batchNumber, Integer userId) {
        return settingMapper.update(batchNumber, userId);// 自己写 sql 一次性修改
    }

    @Transactional //
    public void add(Setting setting) {
        settingMapper.insertSelective(setting);
    }
}
